package com.ineatconseil.yougo.client.ui.main.application.tabs.userTypesMngt;

import com.google.gwt.cell.client.EditTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.view.client.ProvidesKey;
import com.ineatconseil.yougo.client.cto.UserTypeCTO;
import com.ineatconseil.yougo.client.ui.common.cell.ImageActionCell;
import com.ineatconseil.yougo.client.ui.common.constants.YougoConstants;
import com.ineatconseil.yougo.client.ui.common.popup.BasicPopup;
import com.ineatconseil.yougo.client.ui.common.utils.JavascriptObjectUtils;

/**
 * @author Benjamin Barbier - Ineat Conseil
 */
public class UserTypesViewImpl extends Composite implements IUserTypesView {

	@UiTemplate("UserTypesUiBinder.ui.xml")
	interface UserTypesUiBinder extends UiBinder<HTMLPanel, UserTypesViewImpl> {
	}
	
	private static UserTypesUiBinder uiBinder = GWT.create(UserTypesUiBinder.class);
	@UiField Label title;
	@UiField(provided=true) CellTable<UserTypeCTO> cellTable = new CellTable<UserTypeCTO>(KEY_PROVIDER);
	@UiField Button addUserTypeBtn;
	@UiField InlineLabel messageInfo;
	@UiField SimplePager pager;

	public UserTypesViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
		initTableColumns();
	}

	@UiHandler("addUserTypeBtn")
	void onAddUserTypeBtnClick(ClickEvent event) {
		Window.alert("Add user type...");
	}

	@Override
	public void fillTable(int pageSize, JsArray<UserTypeCTO> requests) {
		cellTable.setWidth("100%", false);
		cellTable.setPageSize(pageSize);
		cellTable.setRowData(JavascriptObjectUtils.toList(requests));
		pager.setDisplay(cellTable);
	}
	
	/**
	 * The key provider that allows us to identify UserTypeCTO even if a field
	 * changes. We identify UserTypeCTO by their unique ID.
	 */
	private static final ProvidesKey<UserTypeCTO> KEY_PROVIDER = new ProvidesKey<UserTypeCTO>() {
		@Override
		public Object getKey(UserTypeCTO item) {
			return item.getId();
		}
	};
	
	/**
	 * Initialise les colonnes du tableau.
	 * @see http://code.google.com/intl/fr-FR/webtoolkit/doc/latest/DevGuideUiCellWidgets.html
	 */
	private void initTableColumns() {

		// ID Column
		final Column<UserTypeCTO, String> idColumn = new Column<UserTypeCTO, String>(new TextCell()) {
			@Override
			public String getValue(UserTypeCTO request) {
				return String.valueOf(request.getId());
			}
		};
		idColumn.setSortable(true);
		
		// Description Column
		final Column<UserTypeCTO, String> descriptionColumn = new Column<UserTypeCTO, String>(new EditTextCell()) {
			@Override
			public String getValue(UserTypeCTO request) {
				return request.getDescription();
			}
		};
		descriptionColumn.setSortable(true);
		//TODO: mettre en place un sortHandler pour gérer les tris de colonnes comme ceci :
//		ListHandler<UserTypeCTO> columnSortHandler = new ListHandler<UserTypeCTO>(list);
//		columnSortHandler.setComparator(descriptionColumn,
//				new Comparator<UserTypeCTO>() {
//					public int compare(UserTypeCTO o1, UserTypeCTO o2) {
//						if (o1 == o2) {return 0;}
//						// Compare the description column.
//						if (o1 != null) {
//							return (o2 != null) ? o1.getDescription().compareTo(o2.getDescription()) : 1;
//						}
//						return -1;
//					}
//				});
//		cellTable.addColumnSortHandler(columnSortHandler);
		
		descriptionColumn.setFieldUpdater(new FieldUpdater<UserTypeCTO, String>() {
			@Override
			public void update(int index, UserTypeCTO object, String value) {
		        Window.alert("You changed the name of " + object.getDescription() + " to " + value);
		        object.setDescription(value);
		        //TODO: Save the data !
			}
		});
		
		// Edit Column
		final Column<UserTypeCTO, UserTypeCTO> editColumn = new Column<UserTypeCTO, UserTypeCTO>(
				new ImageActionCell<UserTypeCTO>(YougoConstants.ImagePath.EDIT_ICON,
						new ImageActionCell.Delegate<UserTypeCTO>() {
							@Override
							public void execute(final UserTypeCTO request) {
								BasicPopup.showWarning("Etes vous sur de vouloir modifier le type d'utilisateur "
									+ request.getId() + " - " + request.getDescription() + " ?");
							}
						})) {
			@Override
			public UserTypeCTO getValue(final UserTypeCTO userType) {
				return userType;
			}
		};
		
		// Remove Column
		final Column<UserTypeCTO, UserTypeCTO> removeColumn = new Column<UserTypeCTO, UserTypeCTO>(
				new ImageActionCell<UserTypeCTO>(YougoConstants.ImagePath.TRASH_ICON,
						new ImageActionCell.Delegate<UserTypeCTO>() {
							@Override
							public void execute(final UserTypeCTO userType) {
								BasicPopup.showWarning("Etes vous sur de vouloir supprimer le type d'utilisateur "
									+ userType.getId() + " - " + userType.getDescription() + " ?");
							}
						})) {
			@Override
			public UserTypeCTO getValue(final UserTypeCTO userType) {
				return userType;
			}
		};
		
		this.addColumnOnTable(idColumn, "ID", 50, Unit.PX);
		this.addColumnOnTable(descriptionColumn, "Description", 100, Unit.PCT);
		this.addColumnOnTable(editColumn, "", 33, Unit.PX);
		this.addColumnOnTable(removeColumn, "", 33, Unit.PX);
		
	}
	
	private void addColumnOnTable(final Column<UserTypeCTO, ?> column, final String title, final double columnWidth, Unit unit) {
		cellTable.addColumn(column, title);
		cellTable.setColumnWidth(column, columnWidth, unit);
	}
	
}
